package
{
	import flash.utils.ByteArray;
	import flash.data.*;
	import flash.events.SQLEvent;
	import flash.events.SQLErrorEvent;
	import flash.filesystem.File; 
	
	[Bindable]
	[Table(name='tasks')]
	dynamic class Tasks extends Object
	{
		[Id]
		[Column(name='task_id')]
		public var id:int;
		public var localid:int;
		public var queueid:int;
		public var assigneeid:int;
		public var statusid:int;
		public var projectid:int;
		public var moduleid:int;
		public var title:String;
		public var summary:String;
		public var dateopen:String;
		public var datedue:String;
		public var dateclosed:String;
		public var estimate:String;
		public var ownerid:int;
		public var priorityid:int;
		public var color:String;
		public var priority:String;
		public var severity:String;
		public var status:String;
		public var project:String;
		public var clientid:int;
		public var client:String;
		public var milestoneid:int;
		public var milestone:String;
		public var odule:String;
		public var assignees:String;
		public var followers:String;
		public var followerid:int;
		public var owners:String;
		public var label:String;

		private static var dbFile:File;
		private static var conn:SQLConnection;
		
		public function Tasks()
		{
			if (conn == null) 
			{
				conn = new SQLConnection(); 
					 
				conn.addEventListener(SQLEvent.OPEN, openHandler); 
				conn.addEventListener(SQLEvent.RESULT, getResults); 
				conn.addEventListener(SQLErrorEvent.ERROR, errorHandler); 
					 
				// The database file is in the application storage directory 
				dbFile = File.applicationStorageDirectory.resolvePath("tasks.db"); 
				
				if(!dbFile.exists)
				{
					conn.open(dbFile, SQLMode.CREATE);
					conn.close();
				}
				conn.open(dbFile, SQLMode.UPDATE);
				
				var sqlStatement:SQLStatement = new SQLStatement();
				sqlStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);
				sqlStatement.sqlConnection = conn;
				sqlStatement.text = 'Create Table If Not Exists tasks(id INTEGER unique, localid INTEGER, title TEXT, summary TEXT, datedue TEXT, color TEXT, status TEXT)';
				sqlStatement.execute();
				localid = 0;
			}
		}
		
		public function getResults(e:SQLEvent):void
		{
			//trace(e.target.data);
		}
		
		public function execute_query(sql)
		{
			Tasks(null);
			var sqlStatement:SQLStatement = new SQLStatement();
			sqlStatement.sqlConnection = conn;
			sqlStatement.text = sql;
			sqlStatement.execute();
			return sqlStatement.getResult().data;
		}
		
		public function insert()
		{
			Tasks(null);
			var sqlStatement:SQLStatement = new SQLStatement();
			sqlStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);
			sqlStatement.sqlConnection = conn;
			if(get_task(localid))
			{
				sqlStatement.text = 'Update tasks Set title=@TITLE, summary=@SUMMARY, datedue=@DATEDUE, color=@COLOR, status=@STATUS Where localid = @LOCALID';
			}
			else
			{
				sqlStatement.text = 'Insert Into tasks Values(@ID, @LOCALID, @TITLE, @SUMMARY, @DATEDUE, @COLOR, @STATUS)';
				sqlStatement.parameters['@ID'] = id;
			}
			sqlStatement.parameters['@LOCALID'] = localid;
			sqlStatement.parameters['@TITLE'] = title;
			sqlStatement.parameters['@SUMMARY'] = summary;
			sqlStatement.parameters['@DATEDUE'] = datedue;
			sqlStatement.parameters['@COLOR'] = color;
			sqlStatement.parameters['@STATUS'] = status;
			sqlStatement.execute();
		}
		
		public function update()
		{
			
		}
		
		public function get_task(task_number)
		{
			Tasks(null);
			var sqlStatement:SQLStatement = new SQLStatement();
			sqlStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);
			sqlStatement.sqlConnection = conn;
			sqlStatement.text = 'Select * From tasks Where localid = '+task_number; //@LOCALID';
			//sqlStatement.parameters['@LOCALID'] = task_number;
			sqlStatement.execute();
			var _data = sqlStatement.getResult().data;
			try
			{
				var test = _data[0];
				set_details(_data[0]);
			}
			catch(e)
			{
				new Errors('This is currently a bit shit and requires all the tasks to have come from the task select window for the submit to work...'+"\n"+e.message);
			}
			return(_data);
		}
			 
		function openHandler(event:SQLEvent):void 
		{ 
			//trace("the database was created successfully"); 
		} 
			 
		function errorHandler(event:SQLErrorEvent):void 
		{ 
			new Errors("Error message: "+event.error.message+"\nDetails: "+event.error.details); 
		} 
		
		public function set_details(taskXML)
		{
			id = taskXML.id;
			localid = taskXML.localid;
			queueid = taskXML.queueid;
			assigneeid = taskXML.assigneeid;
			statusid = taskXML.statusid;
			projectid = taskXML.projectid;
			moduleid = taskXML.moduleid;
			title = taskXML.title;
			summary = taskXML.summary;
			dateopen = taskXML.dateopen;
			datedue = taskXML.datedue;
			dateclosed = taskXML.dateclosed;
			estimate = taskXML.estimate;
			ownerid = taskXML.ownerid;
			priorityid = taskXML.priorityid;
			color = taskXML.color;
			priority = taskXML.priority;
			severity = taskXML.severity;
			status = taskXML.status;
			project = taskXML.project;
			clientid = taskXML.clientid;
			client = taskXML.client;
			milestoneid = taskXML.milestoneid;
			milestone = taskXML.milestone;
			module = taskXML.module;
			assignees = taskXML.assignees;
			followers = taskXML.followers;
			followerid = taskXML.followerid;
			owners = taskXML.owners;
			label = '#'+localid+': '+title;
		}
		
		public function get_details()
		{
			return new Object(
				id,
				localid,
				queueid,
				assigneeid,
				statusid,
				projectid,
				moduleid,
				title,
				summary,
				dateopen,
				datedue,
				dateclosed,
				estimate,
				ownerid,
				priorityid,
				color,
				priority,
				severity,
				status,
				project,
				clientid,
				client,
				milestoneid,
				milestone,
				module,
				assignees,
				followers,
				followerid,
				owners
			);
		}
	}
}